/**   
* @Title: PushRiskUnLockLogic.java 
* @Package com.gomeplus.insurancec.logic.impl
* @Description:  推送评测测评定时任务
* @author xinpeilin   
* @date 2017年7月27日 下午4:51:19 
* @company cn.com.gome
* @version V1.0   
*/ 


package cn.com.klun.tjy.logic.impl;

import cn.com.klun.inter.ILogics;
import cn.com.klun.tjy.common.constant.ResultEnum;
import cn.com.klun.tjy.common.util.DateUtils;
import cn.com.klun.tjy.common.util.TaskConstants;
import cn.com.klun.tjy.dao.entity.TblStockDaykInfo;
import cn.com.klun.tjy.dao.mapper.edit.TblStockDaykInfoEditMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/** 
 * @ClassName: DayKUpdateUnlockLogic
 * @Description: 更新日K定时任务解锁
 * @author wangheming
 * @date 2017年7月27日 下午4:51:19  
 */
@Service
public class DayKUpdateUnlockLogic implements ILogics<Map<String, Object>> {
	
	private Logger logger = LoggerFactory.getLogger("DayKUpdateUnlockLogic");
	
	@Resource
	private TblStockDaykInfoEditMapper tblStockDaykInfoEditMapper;

	@Override
	public ResultEnum exec(Map<String, Object> map) throws Exception {
		TblStockDaykInfo tblStockDaykInfo = (TblStockDaykInfo) map.get("tblStockDaykInfo");
		try{
			logger.info("更新日K定时任务解锁开始,日志关联KEY：{}", map.get("_LOG_AND_KEY"));
			Map<String, Object> lockMap = new HashMap<String, Object>();
			int lockCounts = Integer.parseInt(tblStockDaykInfo.getLockCounts()) + 1;
			String lockTm = DateUtils.getCurrDateTime();
			if("true".equals(map.get("processFlag"))){
				lockMap.put("lockFlagNew", TaskConstants.LOCK_FLG_SUCC);
			}else{
				if(lockCounts <= 7){
					lockMap.put("lockFlagNew", TaskConstants.LOCK_FLG_NO);
					//当前时间
					String currDateTime = DateUtils.getCurrDateTime();
					// 推迟锁定时间为  执行次数*执行次数(分钟)
					if(lockCounts < 4){
						lockTm = DateUtils.countTime(currDateTime, lockCounts * lockCounts * 60/2);
					}else{
						lockTm = DateUtils.countTime(currDateTime, lockCounts * lockCounts * 60);
					}
				}else{
					lockMap.put("lockFlagNew", TaskConstants.LOCK_FLG_FAIL);
				}
			}
			lockMap.put("kId", tblStockDaykInfo.getStoId());
			lockMap.put("lockCountsNew", lockCounts+"");
			lockMap.put("lockTimeNew", lockTm);
			lockMap.put("updateTimeNew", DateUtils.getCurrDateTime());
			lockMap.put("lockFlag", TaskConstants.LOCK_FLG_ING);
			int count = tblStockDaykInfoEditMapper.editInfo(lockMap);
			if(count == 1){
				logger.info("更新日K定时任务解锁成功,日志关联KEY：{}，kId：{}",map.get("_LOG_AND_KEY"),tblStockDaykInfo.getStoId());
				return ResultEnum.OK;
			}
		}catch(Exception e){
			logger.error("更新日K定时任务解锁异常,日志关联KEY：{}" ,map.get("_LOG_AND_KEY"),e);
			return ResultEnum.PART_CASE_01;
		}
		return ResultEnum.PART_CASE_01;
	}

}
